﻿using HandyControl.Tools;
using Supore.APP.UI.Controls.Notify;
using SuporeSliplampApp.Models;
using SuporeSliplampApp.Service;
using SuporeSliplampApp.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Xceed.Wpf.Toolkit;

namespace SuporeSliplampApp.Views
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    { 
        EditPatientService editPatientService;
        public MainWindow()
        {
           InitializeComponent();
            var mainViewModel = MainViewModel.mainViewModel;
           this.DataContext = mainViewModel;
            editPatientService = new EditPatientService();
            //datePicker.SelectedDate = DateTime.Now;
            this.Loaded += MainWindow_Loaded;                                           
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            
            editPanel.Height = 0;
            ForbidCursor();
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
            Keyboard.ClearFocus();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (System.Windows.MessageBox.Show("请确定退出?", "警告", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                Application.Current.Shutdown();
            }
        }
        private void CamPage_ContextMenuClosing(object sender, ContextMenuEventArgs e)
        {
            System.Windows.MessageBox.Show("test");
        }

        private void eidtBtn_Click(object sender, RoutedEventArgs e)
        {
            //Button button = sender as Button;
            //ContextMenu ctx = button.ContextMenu;
            //ctx.PlacementTarget = button;
            //ctx.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
            //ctx.IsOpen = true;
        }

        private void eidtBtn_Click_1(object sender, RoutedEventArgs e)
        {
            HidePopup();
        }

        private void editBtn_Click(object sender, RoutedEventArgs e)
        {
            editPanel.Height = 50;
            HidePopup();
            MainViewModel.GetInstance().IsEditEnabled = true;
            ArrowCursor();
        }

        private void closeBtn_Click(object sender, RoutedEventArgs e)
        {
            //testBtn.Height = 0;
            HidePopup();
        }

        private void HidePopup()
        {
            if (!TestPopup.IsOpen)
            {
                TestPopup.IsOpen = true;
            }
            else
            {
                TestPopup.IsOpen = false;
            }
        }

        private void deleteBtn_Click(object sender, RoutedEventArgs e)
        {
            HidePopup();
            ForbidCursor();
        }

        private void barCodeBtn_Click(object sender, RoutedEventArgs e)
        {
            HidePopup();
        }

        private void ForbidCursor()
        {
            this.patientPanel.Cursor = Cursors.No;
            datePicker.Cursor = Cursors.No;
        }

        private void ArrowCursor()
        {
            this.patientPanel.Cursor = Cursors.Arrow;
            datePicker.Cursor = Cursors.Arrow;
        }

        private void Btn_Save_Click(object sender, RoutedEventArgs e)
        {
            SaveData();
        }
        private void SaveData()
        {
            string currentid;
            PatientModel patient = new PatientModel();
            patient.Name = nameTxt.Text;
            patient.P_ID = pIdTxt.Text;
            currentid = pIdTxt.Text;
            patient.Email = mailTxt.Text;
            patient.SEX = sexTxt.Text;
            patient.Adreess = adrTxt.Text;
            patient.Creator = creatorTxt.Text;
            patient.Birth = datePicker.SelectedDate.Value;
            patient.PhoneNum = teleTxt.Text;
            if (!editPatientService.EditPatient(patient,currentid))
            {
                LayUINotify.ShowInfo("保存失败！");
                return;
            }
            LayUINotify.ShowInfo("修改成功！");
            editPanel.Height = 0;

            MainViewModel.GetInstance().patientModel.Name = patient.Name;
            MainViewModel.GetInstance().patientModel.P_ID = patient.P_ID;
            MainViewModel.GetInstance().patientModel.SEX = patient.SEX;
            MainViewModel.GetInstance().patientModel.Str_Birth = patient.Birth.ToShortDateString();
            MainViewModel.GetInstance().patientModel.Birth = patient.Birth;
            MainViewModel.GetInstance().patientModel.Adreess = patient.Adreess;
            MainViewModel.GetInstance().patientModel.PhoneNum = patient.PhoneNum;
            MainViewModel.GetInstance().patientModel.Email = patient.Email;
            MainViewModel.GetInstance().patientModel.Creator = patient.Creator;

        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {

        }

        private void lightControl_Click(object sender, RoutedEventArgs e)
        {
            LightControlView lightControlView = new LightControlView();
            lightControlView.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
            lightControlView.ShowDialog();
        }
    }
}
